package com.album.controller;


import com.album.auto.JSONAuthentication;
import com.album.entity.AlAl;
import com.album.entity.AlDa;
import com.album.entity.AlUser;
import com.album.mapper.AlDaMapper;
import com.album.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author album
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/al-da")
public class AlDaController extends JSONAuthentication {
    @Autowired
    AlDaMapper alDaMapper;
    @ApiOperation(value = "添加用户资料")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody AlDa alDa) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",alDa.getUserId());
        if(alDaMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("用户资料已存在");
            this.WriteJSON(request, response, error);
        }else{
            if(alDaMapper.insert(alDa)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除用户资料")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = alDaMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改用户资料")
    @PostMapping("/updDaMeans")
    public void updDaMeans(HttpServletRequest request, HttpServletResponse response, @RequestBody AlDa alDa) throws Exception {
        if(alDaMapper.updateById(alDa)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户上传的资料")
    @PostMapping("/access")
    public void access(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id) throws Exception {
        AlDa alDa=alDaMapper.selectById(id);
        alDa.setStatus("通过");
        if(alDaMapper.updateById(alDa)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户id查找用户资料")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        AlDa al=alDaMapper.selectOne(wrapper);
        if (al!=null) {
            Result ok = Result.ok().data("user", al);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("用户没有添加资料");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "查找全部资料")
    @PostMapping("/findAllZl")
    public void findAllZl(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<AlDa> page = new Page<>(current, size);
        Page<AlDa> pages = alDaMapper.selectPage(page, null);
        List<AlDa> list = pages.getRecords();
        int total = alDaMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有资料");
        this.WriteJSON(request, response, error);
    }

}

